﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace ConsumoDeAPI.Model
{
    class ServiceModel
    {
       private Uri serviceUri;
       public event EventHandler<ResponseEventArg> ServiceResponseReady;

       public ServiceModel(Uri serviceUri) {
           this.serviceUri = serviceUri;
       }
        

        public void Request() {

            HttpWebRequest httpR = HttpWebRequest.Create(serviceUri) as HttpWebRequest;
            httpR.BeginGetResponse((AR) => {

                WebResponse resp = httpR.EndGetResponse(AR);
                HttpWebResponse response = (HttpWebResponse)resp;
                Stream streamResponse = response.GetResponseStream();
                StreamReader streamRead = new StreamReader(streamResponse);
                string responseString = streamRead.ReadToEnd();
                // Close the stream object
                streamResponse.Close();
                streamRead.Close();
                // Release the HttpWebResponse
                response.Close();
               ServiceResponseReady(this, new ResponseEventArg(responseString));
            },httpR);
      
        }

     
    }
}
